package com.jams.goodox;

import android.content.IntentFilter;

import com.haoyang.newairedu.WXResultBroadcast;
import com.jams.goodox.activity.home.HomeActivity;
import com.jams.goodox.constant.UamaPayCode;
import com.jams.goodox.entity.pay.AiliPayBean;
import com.jams.goodox.entity.pay.BalancePayBean;
import com.jams.goodox.entity.pay.WeChatPayBean;
import com.jams.goodox.listen.PayListener;
import com.jams.goodox.utils.ToastUtil;
import com.jams.goodox.utils.UamaPayUtils;

/**
 * Created by jams on 2017/7/27
 * ***
 * Tel: ***
 * Desc:支付基础类
 * Tip: 继承该类，不能覆盖onDestroy();
 */
public abstract class BasePayActivity extends BaseActivity {
    private WXResultBroadcast wxResultBroadcast;
    private PayListener weiPayListener;

    @Override
    protected void init() {
        bindWXResultBroadcast();
    }

    //余额支付
    public void payByBalance(BalancePayBean balancePayBean,PayListener payListener) {
        UamaPayUtils.payByBalance(this, balancePayBean,payListener);
    }

    /**
     * 支付宝支付
     *
     * 调用该接口时，请仔细区分  充值订单和支付订单
     * 对应的id不能传错
     */
    public void payByAili(AiliPayBean ailiPayBean,PayListener payListener){
        UamaPayUtils.payByAili(this,ailiPayBean,payListener);
    }

    /**
     * 微信支付
     *
     * 调用该接口时，请仔细区分  充值订单和支付订单
     * 对应的id不能传错
     */
    public void payByWeChat(WeChatPayBean weChatPayBean,PayListener payListener){
        weiPayListener = payListener;
        UamaPayUtils.payByWeChat(this,weChatPayBean);
    }

    /**
     * 绑定微信支付的回调
     */
    protected void bindWXResultBroadcast() {
        wxResultBroadcast = new WXResultBroadcast();
        IntentFilter filter = new IntentFilter();
        filter.addAction(WXResultBroadcast.WXRESULT_ACTION);
        registerReceiver(wxResultBroadcast, filter);
        wxResultBroadcast.setWxResultListener(new WXResultBroadcast.WXResultListener() {
            @Override
            public void onSucceed() {
                qStartActivity(HomeActivity.class);
                finish();
            }

            @Override
            public void cancel() {
                ToastUtil.show(TownApplicationLike.getInstance(), "支付取消");
            }

            @Override
            public void onFail() {
                ToastUtil.showShort(TownApplicationLike.getInstance(), "支付失败");
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (wxResultBroadcast != null) {
            unregisterReceiver(wxResultBroadcast);
        }
        super.onDestroy();
    }
}
